package com.hs.sfzy.controller;

import com.github.pagehelper.PageInfo;
import com.hs.sfzy.entity.Article;
import com.hs.sfzy.entity.Charge;
import com.hs.sfzy.entity.Consult;
import com.hs.sfzy.service.impl.ChargeServiceImpl;
import com.hs.sfzy.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @BelongsProject: sfzyBackEnd
 * @BelongsPackage: com.hs.sfzy.controller
 * @ClassName ChargeController
 * @Author: liubo
 * @CreateTime: 2024-10-24  10:00
 * @Description: Controller
 * @Version: 1.0
 */
@RestController
@RequestMapping("/charge")
public class ChargeController {
    @Autowired
    private ChargeServiceImpl chargeService;
    @GetMapping("/findAll")
    public Result selectAll(Charge charge) {
        List<Charge> list = chargeService.selectAll(charge);
        return  Result.success(list);
    }

    @GetMapping("/{id}")
    public Result selectById(@PathVariable("id") int id) {
        Charge charge = chargeService.selectById(id);
        return Result.success(charge);
    }

    @PostMapping("/add")
    public Result insert(@RequestBody Charge charge) {
        int num = chargeService.insert(charge);
        if (num != 0){
            return Result.success("添加成功");
        }else {
            return Result.error("添加失败");
        }
    }
    @GetMapping("/findByPage")
    public Result selectByPage(
            @RequestParam(required = false,defaultValue = "1") int pageNum,
            @RequestParam(required = false,defaultValue = "1") int pageSize,
            Charge charge){
        PageInfo<Charge> info = chargeService.selectByPageInfo(pageNum, pageSize, charge);
        return Result.success(info);
    }

    @PutMapping("/edit")
    public Result update(@RequestBody Charge  charge) {
        int num = chargeService.update(charge);
        if (num != 0){
            return Result.success("修改成功");
        }else {
            return Result.error("修改失败");
        }
    }
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable("id") Integer id) {
        int num = chargeService.delete(id);
        if (num == 1){
            return Result.success("删除成功");
        }else {
            return Result.error("删除失败");
        }
    }
}
